﻿using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.Caching.StackExchangeRedis;
using Volo.Abp.Modularity;
using Xms.Abp.Modularity;

namespace Xms.Abp.Caching;

[DependsOn(
    typeof(XmsAbpCachingModule),
    typeof(AbpCachingStackExchangeRedisModule)
    )]
public class XmsAbpCachingModule : XmsModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddTransient(typeof(IXmsCache<>), typeof(XmsRedisCache<>));
        context.Services.Replace(ServiceDescriptor.Singleton<IDistributedCache>(s => s.GetRequiredService<IXmsCache>()));

        var configuration = context.Services.GetConfiguration();
        Configure<RedisCacheOptions>(options =>
        {
            options.Configuration = configuration.GetConnectionString("DefaultCache");
        });
    }
}

